﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightSdms.FacilitiesService;

namespace SilverlightSdms.UserControls
{
    public partial class UserControlFacilities : UserControl
    {
        private Action m_LoadData;

        public UserControlFacilities()
        {
            InitializeComponent();
        }

        private void LoadAllFacilitiesData()
        {
            var client = new FacilitiesServiceClient();
            busyIndicator.IsBusy = true;

            client.GetFacilitiesCompleted += (s, ea) =>
            {
                dataGridFacilities.AutoGenerateColumns = true;
                dataGridFacilities.ItemsSource = ea.Result;
                busyIndicator.IsBusy = false;
            };

            client.GetFacilitiesAsync();
            m_LoadData = LoadAllFacilitiesData;
        }

        private void LoadMyFacilitiesData()
        {
            var client = new FacilitiesServiceClient();
            busyIndicator.IsBusy = true;

            client.GetFacilitiesByUserCompleted += (s, ea) =>
            {
                dataGridFacilities.AutoGenerateColumns = true;
                dataGridFacilities.ItemsSource = ea.Result;
                busyIndicator.IsBusy = false;
            };

            client.GetFacilitiesByUserAsync(((App)Application.Current).UserId);
            m_LoadData = LoadMyFacilitiesData;
        }

        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            m_LoadData();
        }

        private void buttonShowByRegion_Click(object sender, RoutedEventArgs e)
        {
            LoadMyFacilitiesData();
        }

        private void buttonShowAll_Click(object sender, RoutedEventArgs e)
        {
            LoadAllFacilitiesData();
        }

        private void buttonShowAll_Loaded(object sender, RoutedEventArgs e)
        {
            LoadAllFacilitiesData();
        }
    }
}
